﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Phatent.Model.Functions;
namespace Phatent.Command.Functions
{
    public class MeetingCommand
    {
        public static MeetingCollection Gets()
        {
            var ret = new MeetingCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Meeting> q = db.DataContext.Meetings;
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<MeetingModel, Meeting, MeetingCollection>(lst);
                    ret.TotalRecordsCount = n;
                }
            }
            return ret;
        }

        public static MeetingModel GetTopModel()
        {
            MeetingModel ret = null;
            using (DBCentre db = new DBCentre())
            {
                var d = db.DataContext.Meetings.OrderByDescending(p => p.ID).FirstOrDefault();
                if (d != null)
                    ret = Convertor.ConvertTo<MeetingModel, Meeting>(d);
            }
            return ret;
        }

        public static MeetingModel GetModel(int id)
        {
            MeetingModel ret = null;
            using (DBCentre db = new DBCentre())
            {
                var d = db.DataContext.Meetings.FirstOrDefault(p=>p.ID==id);
                if (d != null)
                    ret = Convertor.ConvertTo<MeetingModel, Meeting>(d);
            }
            return ret;
        }

        public static void Update(MeetingModel model)
        {
            using (DBCentre db = new DBCentre())
            {
                var d = db.DataContext.Meetings.FirstOrDefault(p => p.ID == model.ID);
                if (d != null)
                {
                    Convertor.UpdateValue(model, d);
                    db.DataContext.SaveChanges();
                }
            }
        }
    }
}
